﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiCollisionsValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands;

public class CUiCollisionsValidator : ITxValidator
{
  private TxTypeFilter _filterTypes;
  private TxTypeFilter _filterPartAppearances;
  private TxObjGridCtrl _grid;
  private TxObjGridCtrl _otherGrid;

  public CUiCollisionsValidator(TxObjGridCtrl grid, TxObjGridCtrl otherGrid)
  {
    this._filterTypes = new TxTypeFilter();
    this._filterTypes.AddIncludedType(typeof (ITxComponent));
    this._filterTypes.AddIncludedType(typeof (ITxLogicalCollection));
    this._filterTypes.AddIncludedType(typeof (ITxCompound));
    this._filterTypes.AddIncludedType(typeof (ITxGeometry));
    this._filterTypes.AddIncludedType(typeof (ITxPartAppearance));
    this._filterTypes.AddIncludedType(typeof (TxGroup));
    this._filterTypes.AddIncludedType(typeof (TxTemporaryObject));
    this._filterTypes.AddIncludedType(typeof (ITxPointCloud));
    this._filterTypes.AddIncludedType(typeof (ITxPointCloudLayer));
    this._filterTypes.AddIncludedType(typeof (ITxCable));
    this._filterTypes.AddExcludedType(typeof (ITxMfgFeature));
    this._filterTypes.AddExcludedType(typeof (TxPlcPropertyProjector));
    this._filterTypes.AddExcludedType(typeof (ITxSection));
    this._filterTypes.AddExcludedType(typeof (TxViewCamera));
    this._filterPartAppearances = new TxTypeFilter(new Type[2]
    {
      typeof (ITxPartAppearance),
      typeof (ITxTemporaryObject)
    });
    this._grid = grid;
    this._otherGrid = otherGrid;
  }

  private TxProcessModelId GetProcessModelId(ITxObject obj)
  {
    TxProcessModelId processModelId = (TxProcessModelId) null;
    switch (obj)
    {
      case ITxTemporaryObject _:
        processModelId = ((ITxTemporaryObject) obj).ProcessModelId;
        break;
      case ITxPartAppearance _:
        ITxPlanningObject originPart = ((ITxPartAppearance) obj).OriginPart;
        if (originPart != null)
        {
          processModelId = originPart.ProcessModelId;
          break;
        }
        break;
    }
    return processModelId;
  }

  private TxObjectList FilterDynamicPartAppearances(TxObjectList objects)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objects)
    {
      if (this._filterPartAppearances.DoesPassFilter(itxObject) && !this.IsStaticAppearance(itxObject))
        ((Collection<ITxObject>) txObjectList).Add(itxObject);
    }
    return txObjectList;
  }

  private bool IsStaticAppearance(ITxObject obj)
  {
    bool flag = false;
    if (obj is ITxPartAppearance)
    {
      TxPartPhysicalAppearanceEx physicalAppearanceEx = new TxPartPhysicalAppearanceEx(obj);
      flag = physicalAppearanceEx != null && physicalAppearanceEx.IsStaticAppearance();
    }
    return flag;
  }

  private bool ValidPartAppearanceObject(ITxObject obj, TxObjectList objects)
  {
    TxProcessModelId processModelId1 = this.GetProcessModelId(obj);
    bool flag = TxProcessModelId.op_Inequality(processModelId1, (TxProcessModelId) null);
    if (flag && !this.IsStaticAppearance(obj))
    {
      foreach (ITxObject dynamicPartAppearance in (Collection<ITxObject>) this.FilterDynamicPartAppearances(objects))
      {
        TxProcessModelId processModelId2 = this.GetProcessModelId(dynamicPartAppearance);
        if (TxProcessModelId.op_Inequality(processModelId2, (TxProcessModelId) null) && processModelId1.ExternalId == processModelId2.ExternalId)
        {
          flag = false;
          break;
        }
      }
    }
    return flag;
  }

  private bool IsPartAppearance(ITxObject obj) => this._filterPartAppearances.DoesPassFilter(obj);

  public bool IsValidObject(ITxObject obj, TxObjectList objectList, out string errorMessage)
  {
    errorMessage = string.Empty;
    bool flag = this._filterTypes.DoesPassFilter(obj);
    if (flag && this.IsPartAppearance(obj))
      flag = this.ValidPartAppearanceObject(obj, this._grid.Objects);
    return flag;
  }

  public ITxObject GetObject(string text) => throw new NotImplementedException();

  public string GetText(ITxObject obj) => obj.Name;

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    return this.IsValidObject(obj, this._otherGrid.Objects, out errorMessage);
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    throw new NotImplementedException();
  }
}
